﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Mvc;

namespace DFGJ.WMS_PDA.WebAPI.Controllers.PDA
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PDAEmptyTrayController : Controller
    {
        private readonly IPDAEmptyTrayRepository trayRepository;
        public PDAEmptyTrayController(IPDAEmptyTrayRepository trayRepository)
        {
            this.trayRepository = trayRepository;
        }

        /// <summary>
        /// 空托盘出入库
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult EmptyPalletInboundAndOutbound(PDAEmptyTrayDto dto)
        {
                try
                {
                string msg = "";
                if(dto.Type == "Stacking")
                {
                    var updOtherData = trayRepository.UpdEmptyTrayStatus(dto,out msg);
                    if (updOtherData)
                    {
                        return Ok(BaseResult.Result(true, "入库成功", "200"));
                    }
                    //向WMS返回信息
                    return Ok(BaseResult.Result(false, msg, "500"));
                }
                else
                {
                    return Ok(BaseResult.Result(false, "指令类型有误", "400"));
                }
            }
            catch
            {
                return Ok(BaseResult.Result(false, "更新失败", "500"));
            }
        }

        /// <summary>
        /// 获取所有库房
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllWarehouse()
        {
            try
            {
                var list = trayRepository.GetAllWarehouse();
                if (list.Count()>0)
                {
                    return Ok(BaseResult.Result(list, "查询成功", "200"));
                }
                return Ok(BaseResult.Result(false, "查询失败", "500"));
            }
            catch
            {
                return Ok(BaseResult.Result(false, "更新失败", "500"));
            }

        }



        /// <summary>
        /// 添加入库单
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <param name="WarehouseCode"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddEmptyTrayReceivingOrder(string PalletBarcode, string WarehouseCode)
        {
            try
            {
                var list = trayRepository.AddEmptyTrayReceivingOrder(PalletBarcode, WarehouseCode, out string msg);
                if (list)
                {
                    return Ok(BaseResult.Result(true, "更新成功", "500"));
                }
                return Ok(BaseResult.Result(false, msg, "500"));
            }
            catch
            {
                return Ok(BaseResult.Result(false, "更新失败", "500"));
            }

        }
    }
}
